package com.syqc.monitor.ui.cost

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.activityViewModels
import androidx.viewbinding.ViewBinding
import autodispose2.autoDispose
import com.luck.picture.lib.entity.LocalMedia
import com.syqc.comlib.base.LazyFragment
import com.syqc.custom.UnitEditText
import com.syqc.comlib.custom.timer.CustomDatePicker
import com.syqc.comlib.custom.timer.DateFormatUtils
import com.syqc.monitor.ui.newaddcar.PhotoImagePopup
import com.syqc.utils.TimeUtil
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

abstract class BaseFeeFragment<VB : ViewBinding>(
    inflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
) : LazyFragment<VB>(inflater) {

    private val mActVM by activityViewModels<CarFeeViewModel>()

    abstract val isOil: Boolean

    private val mPhotoPopup by lazy { PhotoImagePopup(requireActivity(), 1, this::callback) }

    private val mUploadPopup by lazy {
        UploadPopup(requireContext()) {
            if (it) {
                clearData()
            } else {
                uploadData()
            }
        }
    }

    override fun initData() {
        mViewModel.toastObserver().subscribeOn(AndroidSchedulers.mainThread())
            .autoDispose(scopeProvider).subscribe(this::toastObserver)

        mActVM.clearLive.observe(this) {
            clearData()
        }
        mActVM.carIdLive.observe(this) {
            mViewModel.carId = mActVM.carId
        }
        //提交结果监听
        mViewModel.resultLive.observe(this) {
            mUploadPopup.show(requireActivity().window.decorView, it)
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.carId = mActVM.carId
    }

    protected val mDatePicker: CustomDatePicker by lazy {
        CustomDatePicker(
            requireContext(), callback, TimeUtil.simpleDate,
            TimeUtil.instance.simpleTime(System.currentTimeMillis()), false
        ).apply {
            setCanShowPreciseTime(false)
        }
    }

    private val callback: CustomDatePicker.Callback = object : CustomDatePicker.Callback {
        override fun onTimeSelected(timestamp: Long) {
            val time: String = DateFormatUtils.long2Str(timestamp, false)
            setDate(mViewModel.dataType, time)
            mDatePicker.dismiss()
        }
    }

    /**
     * 设置输入
     *
     * @param edit
     */
    fun focusChange(edit: EditText) {
        edit.setOnFocusChangeListener { _, hasFocus ->
            if (!hasFocus) {
                val toString = edit.text.toString()
                if (toString.endsWith(".")) edit.setText("${toString}0")
            }
        }
    }

    /** 如果"."在起始位置,则起始位置自动补0 */
    fun isNumOk(edit: UnitEditText, str: String): String {
        if (str.startsWith(".")) {
            val s = "0$str"
            val editText = edit.getEditText()
            editText.setText(s)
            editText.setSelection(s.length)
            return s
        }
        val indexOf = str.indexOf(".")
        if (indexOf != -1 && indexOf != (str.length - 1) && str.last() == '.') {
            val s = str.substring(0, str.length - 1)
            val editText = edit.getEditText()
            editText.setText(s)
            editText.setSelection(s.length)
            return s
        }
        return str
    }

    /**
     * 拍照
     *
     */
    fun takePhoto() {
        if (isOil) {
            mPhotoPopup.showAtLocation(1)
        } else {
            mPhotoPopup.showAtLocation(mViewModel.nowNum)
        }

    }

    private fun callback(type: Int, result: ArrayList<LocalMedia>?) {
        if (isOil) {
            mViewModel.handlePhoto(result)
        } else {
            mViewModel.handlePhotos(result)
        }

    }

    abstract val mViewModel: BaseFeeViewModel
    abstract fun setDate(type: Int, time: String)
    abstract fun uploadData()
    abstract fun clearData()

}